//
//  StatusListViewModel.swift
//  weibo
//
//  Created by 谭仁川 on 15/12/4.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
class StatusListViewModel: NSObject {
    class func loadData(finished: (statuses: [Status]?) -> () ) {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let AFN = AFHTTPSessionManager()
        guard let token = UserAccountViewModel().token else {
            SVProgressHUD.showErrorWithStatus("您暂未登录")
            return
        }
        let parameters = ["access_token":token]
        AFN.GET(urlString, parameters: parameters, success: { (_, result) -> Void in
            
            guard let array = result["statuses"] as? [[String : AnyObject]] else {
                
                print("数据类型有误")
                finished(statuses: nil)
                return
            }
            var list = [Status]()
            for item in array {
                let s = Status(dict: item)
                list.append(s)
                
                
            }
            
            finished(statuses: list)
            }) { (_, error) -> Void in
                finished(statuses: nil)
                print(error)
        }
        
    }

}
